package org.xhy.sniffer.boot;

import java.util.*;

public enum ServiceManager {

    INSTANCE;

    private HashMap<Class, BootService> serviceMap = new HashMap<>();


    // 加载 BootService 下的所有实现类
    public void loadAllServices(Properties properties){
        List<BootService> serviceList = new ArrayList<>();
        load(serviceList);
        HashMap<Class, BootService> serviceMap = new HashMap<>();
        for (BootService bootService : serviceList) {
            serviceMap.put(bootService.getClass(), bootService);
            bootService.boot(properties);;
        }
        this.serviceMap = serviceMap;
    }

    void load(List<BootService> serviceList){
        ServiceLoader<BootService> services = ServiceLoader.load(BootService.class);
        for (BootService service : services) {
            serviceList.add(service);
        }
    }


    public BootService getService(Class key){
        return serviceMap.get(key);
    }

    public List<BootService> getServices(){
        return new ArrayList<>(serviceMap.values());    
    }
}
